package com.homework.util;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * 密码工具类
 */
public class PasswordUtil {

    private static final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    /**
     * 生成密码哈希
     */
    public static String encodePassword(String rawPassword) {
        return passwordEncoder.encode(rawPassword);
    }

    /**
     * 验证密码
     */
    public static boolean matches(String rawPassword, String encodedPassword) {
        return passwordEncoder.matches(rawPassword, encodedPassword);
    }

    /**
     * 测试密码
     */
    public static void main(String[] args) {
        String password = "admin123";
        String encodedPassword = "$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iYqiSfFDYZt/I5/BFnhkSLsVBDSC";

        System.out.println("原始密码: " + password);
        System.out.println("存储的哈希: " + encodedPassword);
        System.out.println("验证结果: " + passwordEncoder.matches(password, encodedPassword));

        // 生成新的哈希
        String newEncoded = encodePassword(password);
        System.out.println("新生成的哈希: " + newEncoded);
        System.out.println("新哈希验证: " + passwordEncoder.matches(password, newEncoded));
    }
}